import {
	url
} from "./url.js"
export default {
	// 全局配置
	common: {
		baseUrl: url + '/',
		header: {
			'Content-Type': 'application/json;charset=UTF-8',
			'Content-Type': 'application/x-www-form-urlencoded'
		},
		data: {},
		method: 'GET',
		dataType: 'json'
	},

	// 请求 返回promise
	request(options = {}) {
		// 组织参数
		options.url = this.common.baseUrl + options.url
		options.header = options.header || this.common.header
		options.data = options.data || this.common.data
		options.method = options.method || this.common.method
		options.dataType = options.dataType || this.common.dataType

		let token = uni.getStorageSync("token");
		if (token) options.data.token = token
		options.data.xitong = uni.$u.os()
		let sys = uni.getSystemInfoSync()
		options.data.pingtai = sys.uniPlatform
		options.data.apptype = 'user'
		console.log("2111111111",options);
		// let wanlLogin = uni.getStorageSync("wanlshop:user");
		// if (wanlLogin) {
		// 	options.header.token = uni.getStorageSync("wanlshop:user").token;
		// }
		// 请求
		return new Promise((res, rej) => {
			// 请求之前... todo
			// 请求中...
			uni.showLoading({
				title: "加载中",
				mask: true
			})
			uni.request({
				...options,
				success: (result) => {
					uni.hideLoading()
					// if(result.statusCode == 401) {
					// 	uni.reLaunch({
					// 		url: "/pages/my/my"
					// 	})
					// 	uni.showToast({ title: "请登录", icon: "none"})
					// 	return
					// }
		console.log("3333333",result);
					if (result.statusCode == 200 || result.statusCode == 401) {
						console.log(result);
						if(result.data.code == 401){
							uni.removeStorageSync('token')
							// if(uni.getStorageSync('login401')){
							// 	return
							// }
							// uni.setStorageSync('login401',1)
							// uni.navigateTo({
							// 	url:'/pages/login/login'
							// })
						}
						res(result.data)
					}
				},
				fail: (error) => {
					uni.hideLoading()
					uni.showToast({
						title: error.errMsg || '请求失败',
						icon: 'none'
					});
					return rej()
				}
			});
		}).catch((e) => {});
	},
	// get请求
	get(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'GET'
		return this.request(options)
	},
	// post请求
	post(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'POST'
		return this.request(options)
	},
	// delete请求
	del(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'DELETE'
		return this.request(options)
	},
	// options请求
	options(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'options'
		return this.request(options)
	},
	// put请求
	put(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'put'
		return this.request(options)
	},
	// head请求
	head(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'head'
		return this.request(options)
	},
	// trace请求
	trace(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'trace'
		return this.request(options)
	},
	// connect请求
	connect(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'connect'
		return this.request(options)
	},
}
